<template>
  <div class="login" :style="{backgroundImage:'url('+image+')'}">
    <div class="image">
      <el-image :src="image" style="width: 100%"/>
    </div>
    <div class="form">
      <div class="box">
        <div style="margin-bottom: 40px">
          <span style="font-size: 35px;font-weight: 600;color: white">宿舍管理系统</span>
        </div>
        <el-form :model="form" class="login_form" :rules="rules" ref="login">
          <el-form-item></el-form-item>
          <el-form-item label="用户名:" prop="userName">
            <el-input v-model="form.userName"/>
          </el-form-item>
          <el-form-item label="密&#8195;码:" prop="password">
            <el-input v-model="form.password" type="password" autocomplete="off"/>
          </el-form-item>
          <el-form-item>
            <div style="display: flex;justify-content: space-between;width: 100%">
              <el-button type="primary" @click="pushLogin" style="width: 47%">登录</el-button>
              <el-button @click="pushRegister" style="width: 47%">注册</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

import {login, getInfo} from "@/api/api";
import {ElMessage} from "element-plus";

export default {
  name: "login",
  data() {
    return {
      rules: {
        userName: [{
          required: true,
          message: "用户名不能为空",
          trigger: "blur"
        }],
        password: [{
          required: true,
          message: "密码不能为空",
          trigger: "blur"
        }]
      },
      image: require("@/assets/login.png"),
      form: {
        userName: "",
        password: ""
      }
    }
  },
  methods: {
    pushLogin() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          login(this.form).then((res) => {
            if (res.data == "用户名或密码不存在!") {
              ElMessage({
                message: res.data,
                type: "error"
              })
            } else {
              ElMessage({
                message: "登录成功!",
                type: "success"
              })
              localStorage.setItem('token', res.data)
              this.$router.push("/userInfo")
            }
          })
        } else {
          ElMessage({
            message: "请输入用户名密码",
            type: "error"
          })
        }
      })
    },
    pushRegister() {
      this.$router.push("/register")
    }
  }
}
</script>

<style scoped>

.image {
  width: 100%;
  height: 100%;
}

.box {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.form {
  width: 20%;
  position: absolute;
  top: 200px;
  right: 39.7%;
}

.login_form {
  width: 100%;
  padding: 1rem;
  background: rgba(255, 255, 255, 0.8);
}

::v-deep .el-image__inner {
  height: 749px !important;
}

::v-deep .el-form-item__label {
  width: 70px !important;
  font-weight: 600;
}

::v-deep .el-form-item__content {
  margin-left: 0px !important;
}


</style>